#include "video_size_measure.h"
#include "rtc_base/logging.h"
namespace webrtc {
void VideoSizeMeasure::SetScaleType(webrtc::VideoScaleMode h_match_scale_type,
                                    webrtc::VideoScaleMode h_mismatch_scale_type,
                                    webrtc::VideoScaleMode v_match_scale_type,
                                    webrtc::VideoScaleMode v_mismatch_scale_type) {
  scaling_type_match_orientation_h_ = h_match_scale_type;
  scaling_type_mismatch_orientation_h_ = h_mismatch_scale_type;
  scaling_type_match_orientation_v_ = v_match_scale_type;
  scaling_type_mismatch_orientation_v_ = v_mismatch_scale_type;
}

std::pair<int, int> VideoSizeMeasure::Measure(int surface_width,
                                                int surface_height,
                                                int frame_width,
                                                int frame_height) {
  float frame_aspect = static_cast<float>(frame_width) / frame_height;
  float display_aspect = static_cast<float>(surface_width) / surface_height;
  // webrtc::VideoScaleMode match_scaling_type = (frame_width > frame_height) ? scaling_type_match_orientation_h_ : scaling_type_match_orientation_v_;
  // webrtc::VideoScaleMode mismatch_scaling_type = (frame_width > frame_height) ? scaling_type_mismatch_orientation_h_ : scaling_type_mismatch_orientation_v_;
  // webrtc::VideoScaleMode scaling_type = (frame_aspect > 1.0f) == (display_aspect > 1.0f) ? match_scaling_type : mismatch_scaling_type;
  webrtc::VideoScaleMode scaling_type = webrtc::VideoScaleMode::kModeScaleAspectFit;
  return GetDisplaySize(scaling_type, frame_aspect, surface_width, surface_height);
}

std::pair<int, int> VideoSizeMeasure::GetDisplaySize(webrtc::VideoScaleMode scaling_type,
                                                       float video_aspect_ratio,
                                                       int max_display_width,
                                                       int max_display_height) {
  return GetDisplaySize(ConvertScaleTypeToVisibleFraction(scaling_type), video_aspect_ratio, max_display_width, max_display_height);

}

std::pair<int, int> VideoSizeMeasure::GetDisplaySize(float min_visible_fraction,
                                                       float video_aspect_ratio,
                                                       int max_display_width,
                                                       int max_display_height) {
  if (min_visible_fraction == 0 || video_aspect_ratio == 0) {
    return {max_display_width, max_display_height};
  }
  int width = std::min(max_display_width, static_cast<int>(std::round(max_display_height / min_visible_fraction * video_aspect_ratio)));
  int height = std::min(max_display_height, static_cast<int>(std::round(max_display_width / min_visible_fraction / video_aspect_ratio)));
  return {width, height};
}

float VideoSizeMeasure::ConvertScaleTypeToVisibleFraction(webrtc::VideoScaleMode scaling_type) {
  switch (scaling_type) {
    case webrtc::VideoScaleMode::kModeScaleAspectFit:
      return 1.0f;
    case webrtc::VideoScaleMode::kModeScaleAspectFill:
      return 0.0f;
    case webrtc::VideoScaleMode::kModeScaleToFill:
    default:
      return 0.5625f;
  }
}
}